//
//  SheZhiViewController.m
//  GOGOTravel
//
//  Created by dllo on 16/3/1.
//  Copyright © 2016年 果果. All rights reserved.
//

#import "SheZhiViewController.h"
#import "DailyFeatureStoryViewController.h"
#import "ShouchangCollectionViewCell.h"
#import "ShouChangViewController.h"
@interface SheZhiViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UIGestureRecognizerDelegate>

@property (nonatomic, retain) UILongPressGestureRecognizer *longpress;
@property (nonatomic, retain) BaseCollectionView *collection;
@property (nonatomic, retain) ShouchangCollectionViewCell *cell;
@end

@implementation SheZhiViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)dealloc{
    [_arr release];
    [_longpress release];
    [_collection release];
    [_cell release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    self.navigationItem.title = @"我的收藏";
    self.view.backgroundColor = [UIColor whiteColor];
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(itemAction:)];
    
    self.navigationItem.rightBarButtonItem = item;
    [item release];
   
    
    
    if (_arr.count == 0) {
        
    
        
        UIImageView *image = [[UIImageView alloc] init];
        image.frame = CGRectMake(0, 0, 100, 100);
        image.center = self.view.center;
        image.image = [UIImage imageNamed:@"iconfont-yewuyeicon05.png"];
       
        
        [self.view addSubview:image];
        [image release];
        
    }else{
        [self initTableView];
    }
}
/** 创建提示alert */
- (void)itemAction:(UIBarButtonItem *)item{
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"长按删除" preferredStyle:UIAlertControllerStyleAlert];
    
    
    [UIView animateWithDuration:2 animations:^{
        [self presentViewController:alert animated:YES completion:^{
            
            
        }];
        
    }];
    
    [UIView animateWithDuration:2 animations:^{
       
        [alert dismissViewControllerAnimated:YES completion:^{
            
        }];
        
    }];
   
    
}

- (void)initTableView{
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    layout.itemSize = CGSizeMake((WIDTH - 30) / 2, 250);
    layout.minimumInteritemSpacing = 10;
    layout.minimumLineSpacing = 10;
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    self.collection = [[BaseCollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) collectionViewLayout:layout];
    _collection.delegate = self;
    _collection.dataSource = self;
    _collection.bounces = NO;
    
    
    
    [self.view addSubview:_collection];
    
    [_collection release];
    //注册
    [_collection registerClass:[ShouchangCollectionViewCell class] forCellWithReuseIdentifier:@"ShouchangCollectionViewCell"];
    
    
    
    
  
    
    
}



- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _arr.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    self.cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ShouchangCollectionViewCell"  forIndexPath:indexPath];
    
    ShouChangModel *model = _arr[indexPath.row];
    _cell.backgroundColor = [UIColor whiteColor];
    _cell.model = model;
    _cell.tag = indexPath.row;
    _cell.layer.cornerRadius = 5;
    _cell.layer.masksToBounds = YES;
    self.longpress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(deleteShouChang:)];
    _longpress.minimumPressDuration = 1;
    _longpress.delegate = self;
    
    [_cell addGestureRecognizer:_longpress];
    [_longpress release];
    
    return _cell;
}
/** 删除收藏数据 */
- (void)deleteShouChang:(UILongPressGestureRecognizer *)longPress{
   
    if (longPress.state == UIGestureRecognizerStateBegan) {
         NSLog(@"删除");
        
        NSInteger row = longPress.view.tag;
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"您确定要删除么" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            
            ShouChangModel *model = _arr[row];
            
            
            [[DbHandel shareDbHandel] deleteDailyModel:model];
            [_arr removeObjectAtIndex:row];
            
            [_collection reloadData];
            
            
            
        }];
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"不删除" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        
        [alert addAction:action];
        [alert addAction:action1];
        
        [self presentViewController:alert animated:YES completion:^{
            
        }];
        
        
        
    }
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    //取出项目的sopt_id 传到下一个页面
    ShouChangModel *model = _arr[indexPath.row];
    NSLog(@"11111111%@", model.spot_id);
    ShouChangViewController *shouChang = [[ShouChangViewController alloc] init];
    
    shouChang.url = [NSString stringWithFormat:@"%@", model.spot_id];
    
    [self.navigationController pushViewController:shouChang animated:YES];
    [shouChang release];
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
